package com.jfsnpm.jfsnpm.core.web.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jfinal.upload.UploadFile;
import com.jfsnpm.jfsnpm.core.dao.User;
import com.jfsnpm.jfsnpm.core.service.UserService;
import com.jfsnpm.jfsnpm.core.util.AppHelper;
import com.jfsnpm.jfsnpm.core.util.JfsnpmException;
import com.jfsnpm.jfsnpm.plugin.bjui.BjuiRender;
import com.jfsnpm.jfsnpm.plugin.bjui.JfsnpmFileRender;
import com.jfsnpm.jfsnpm.plugin.signpdf.signPdf;

public class FileController extends Controller {
	public void upload(){
		User userinfor = UserService.getUserInfor(this);
		UploadFile uploadFile = getFile();
		File file = uploadFile.getFile();
		String fileType = getPara("type","public");
		String releKey = getPara("rele","xxx");
		String fileName = getPara("fileName","none");
		String fileId = AppHelper.getUUID();
		String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
		File filePath = new File(uploadFile.getUploadPath()+fileType+File.separator+releKey+File.separator);
		if(!filePath.exists()){
			filePath.mkdirs();
		}
		//移动位置
		File fileNew = new File(uploadFile.getUploadPath()+fileType+File.separator
				+releKey+File.separator+fileId+"."+fileExt);
		file.renameTo(fileNew);
		
		Record record = new Record().set("id", fileId)
				.set("fileName", fileName).set("fileType", fileType)
				.set("releKey", releKey).set("modifier", userinfor.getUserId())
				.set("modifyDate", AppHelper.getNow())
				.set("filePath", fileNew.getPath());
		if(Db.save("jfsnpm_file", record)){
			render(BjuiRender.success("上传成功", false));
		}else{
			render(BjuiRender.error("上传失败"));
		}
		
	}
	public void download(){
		String id = getPara("id");
		if(AppHelper.isEmpty(id)){
			renderHtml("{\"statusCode\":\"300\",\"message\":\"参数有误\"}");
			return;
		}
		Record record = Db.findById("jfsnpm_file", id);
		String filePath = record.getStr("filePath");
		String fileName = record.getStr("fileName");
		if(AppHelper.isEmpty(filePath)){
			renderHtml("{\"statusCode\":\"300\",\"message\":\"文件地址有误\"}");
			return;
		}
		File file = new File(filePath);
		if (file == null || !file.exists()||file.isDirectory()){
			renderHtml("{\"statusCode\":\"300\",\"message\":\"文件没有找到\"}");
			return;
		}
		render(new JfsnpmFileRender(file, fileName));
	}
	//PDF文件签名
	public void filesign(){
		User userinfor = UserService.getUserInfor(this);
		if(AppHelper.isEmpty(userinfor.getUserId())||
				AppHelper.isEmpty(userinfor.getUserMail())){
			render(BjuiRender.error("用户信息不完整"));
			return;
		}
		String id = getPara("id");
		if(AppHelper.isEmpty(id)){
			render(BjuiRender.error("参数有误"));
			return;
		}
		Record record = Db.findById("jfsnpm_file", id);
		String filePath = record.getStr("filePath");
		String fileName = record.getStr("fileName");
		if(AppHelper.isEmpty(filePath)){
			render(BjuiRender.error("文件地址有误"));
			return;
		}
		String prefix = fileName.toLowerCase().substring(fileName.lastIndexOf(".")+1);
		if(!"pdf".equals(prefix)){
			render(BjuiRender.error("只能对PDF文件进行签名"));
			return;
		}
		File file = new File(filePath);
		if (file == null || !file.exists()||file.isDirectory()){
			render(BjuiRender.error("文件没有找到"));
			return;
		}
		//String fileNameSign = fileName.substring(0, fileName.length()-4)+"_sign.pdf";
		String idSign = AppHelper.getUUID();
		String fileNameSignL = idSign+".pdf";
		File fileSign = new File(file.getParent()+File.separator+fileNameSignL);
		try {
			//File fileimg = new File("");
			signPdf sign = new signPdf(userinfor.getUserMail(), userinfor.getUserId(), file,fileSign);
			//sign.setVisible(true, fileimg, 0, 0, 1, 1);
			sign.signature();
		} catch (Throwable e) {
			throw new JfsnpmException(e.getMessage());
		}
		record//.set("id", idSign).set("fileName", fileNameSign)
			.set("filePath", fileSign.getPath())
			.set("modifier", userinfor.getUserId())
			.set("modifyDate", AppHelper.getNow());
		if(Db.update("jfsnpm_file", record)&&file.delete()){
			render(BjuiRender.success("签名成功！", false));
		}else{
			render(BjuiRender.error("签名成功，但插入数据库文件记录失败！"));
		}
	}
	//简单文件查看删除
	public void list() throws UnsupportedEncodingException{
		String path=getPara("path","");
		path = URLDecoder.decode(path, "UTF-8");
		String basePath = new File(JFinal.me().getConstants().getBaseUploadPath()).getPath();
		if(AppHelper.isEmpty(path)){
			path = basePath;
		}
		if(path.indexOf(basePath)!=0){
			render(BjuiRender.error("文件路径不合法！", false));
			return;
		}
		//文件查看
		File d = new File(path);
		if(!d.exists()){
			render(BjuiRender.error("文件路径不存在！", false));
			return;
		}
		if(d.isDirectory()){
			File list[] = d.listFiles();
			setAttr("list", list);
			setAttr("parentPath",d.getParent());
			setAttr("thisPath","\\"+d.getPath().substring(basePath.length()));
		}else{
			renderFile(d);
			return;
		}
	}
	public void listdelete() throws UnsupportedEncodingException{
		String path=getPara("path","");
		path = URLDecoder.decode(path, "UTF-8");
		String basePath = new File(JFinal.me().getConstants().getBaseUploadPath()).getPath();
		if(AppHelper.isEmpty(path)){
			path = basePath;
		}
		if(path.indexOf(basePath)!=0){
			render(BjuiRender.error("路径不合法！", false));
			return;
		}
		File d = new File(path);
		if(!d.exists()||d.isDirectory()){
			render(BjuiRender.error("路径不存在！", false));
			return;
		}
		if(!d.delete()){
			render(BjuiRender.error("文件删除失败！", false));
			return;
		}
		render(BjuiRender.success("删除成功！", false));
	}
	
	//系统管理的文件管理
	public void filelist(){
		String type = getPara("type");
		String id = getPara("rele","xxx");
		String auth = getPara("auth","");
		if(!(!AppHelper.isEmpty(type)||
				"public".equals(type)||
				"private".equals(type)||
				"project".equals(type)||
				"org".equals(type))){
			render(BjuiRender.error("没有指定正确的类型"));
			return;
		}
		setAttr("type", type);
		setAttr("rele", id);
		if("project".equals(type)){
			setAttr("auth",auth);
			render("filelist_project.html");
		}
	}
	public void filelist_get(){
		String type = getPara("type");
		String id = getPara("rele","xxx");
		if(!(!AppHelper.isEmpty(type)||
				"public".equals(type)||
				"private".equals(type)||
				"project".equals(type)||
				"org".equals(type))){
			render(BjuiRender.error("没有指定正确的类型"));
			return;
		}
		renderJson(Db.find("select * from jfsnpm_file where fileType = ? and releKey = ? ", type,id));
	}
	@Before(Tx.class)
	public void filelist_delete(){
		String json = getPara("json");
		List<HashMap> list = AppHelper.getObjectsFromJson(json, HashMap.class);
		for(Map<String,Object> data:list){
			String id = (String) data.get("id");
			String filePath = Db.findById("jfsnpm_file", id).getStr("filePath");
			Db.update("DELETE FROM jfsnpm_file WHERE id = ?", id);
			File file = new File(filePath);
			if (file == null || !file.exists()){
				
			}else{
				if(!file.delete()){
					throw new JfsnpmException("删除文件失败");
				}
			}
		}
		render(BjuiRender.success("删除成功", false));
	}
	
}
